package cn.baimo.controller;

import cn.baimo.pojo.*;
import cn.baimo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/hospital")
public class HospitalController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalDoctorNumberService hospitalDoctorNumberService;
    @Autowired
    private DivisionService divisionService;
    @Autowired
    private RegistrationService registrationService;
    @Autowired
    private DoctorService doctorService;

    @RequestMapping("/select")
    public String select(Hospital hospital1,String order,Model model,String search,HttpSession session){
        System.err.println("进入：/hospital/select");
        String level1="";
        if (hospital1.getName()!=null && !hospital1.getName().equals(""))
            model.addAttribute("search",hospital1.getName());
        if(hospital1.getLevel()==null||hospital1.getLevel().equals("全部")||hospital1.getLevel().equals("")){
            level1="全部";
            hospital1.setLevel(null);
        }else {
            level1=hospital1.getLevel();
        }
        //查询医院
        List<Hospital> hospitals = hospitalService.selectHospitals(hospital1,order);
        List<HospitalDoctorNumber> hospitalDoctorNumbers = hospitalDoctorNumberService.selectHospitalDoctorNumber();
        for (Hospital hospital : hospitals) {
            for (HospitalDoctorNumber hospitalDoctorNumber : hospitalDoctorNumbers) {
                if (hospitalDoctorNumber.getName().equals(hospital.getName())){
                    hospital.setNum(hospitalDoctorNumber.getNum());
                }
            }
        }
        model.addAttribute("hospitals",hospitals);
        session.setAttribute("level1",level1);
        session.setAttribute("search",search);
        return "/pages/hospital_list.jsp";
    }
    @RequestMapping("/selectById")
    public String selectById(Hospital hospital1,String order,Model model,String search){
        System.err.println("进入：/hospital/selectById");
        Hospital hospital2 = hospitalService.selectHospitalByHospitalId(hospital1.getId());
        //查询医院
        List<HospitalDoctorNumber> hospitalDoctorNumbers = hospitalDoctorNumberService.selectHospitalDoctorNumber();
        for (HospitalDoctorNumber hospitalDoctorNumber : hospitalDoctorNumbers) {
            if (hospitalDoctorNumber.getName().equals(hospital2.getName())){
                hospital2.setNum(hospitalDoctorNumber.getNum());
                hospital2.setNum(registrationService.selectRegistrationTotalNumByHid(hospital2.getId()));
                break;
            }
        }
        //查询医生
        List<Doctor> doctors = doctorService.selectDoctorsByDoctor(new Doctor(), null, hospital2);
        model.addAttribute("hospital",hospital2);
        model.addAttribute("doctors",doctors);
        return "/pages/hospital_detail.jsp";
    }
}
